/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import type { ProColumns } from '@ant-design/pro-table';
import { history } from 'umi';
import { getSmsList, upgradation, operate } from '@/services/sms/examine';
import { message } from 'antd';
import type { TablePaginationConfig } from 'antd/es/table';
import type { PaginationConfig } from 'antd/lib/pagination';
import { usePaginationCache } from '@base/components/keep-alive/_utils';
import { isArray } from 'lodash';

export const userLevel = {
  untrusted: '受限用户',
  normal: '普通用户',
  trusted: '可信用户',
};

export const upTxtMap = {
  untrusted: '是否标为普通用户，并通过该用户所有待审核短信?',
  normal: '是否标为可信用户，并通过该用户所有待审核短信?',
};

export const statusMap = {
  wait: {
    label: '待审',
  },
  pass: {
    label: '审核通过',
  },
  reject: {
    label: '拒绝',
  },
  black: {
    label: '拉黑该账号',
  },
  shield: {
    label: '扣钱不发',
  },
};

export function replaceKeys(content: string, data: any, keys: string[]) {
  if (!content) return '';
  let str: string = content;
  data.NO = data.BH;
  const arr = str.match(/#[A-Z]{1,}#/g);
  if (arr && arr.length > 0) {
    arr.forEach((k: string) => {
      const realKey = k.replaceAll('#', '');
      let v = data[realKey];
      if (data[realKey]) {
        // 部分变量取前三个，格式化显示
        if ((realKey === 'NO' || realKey === 'DH') && v) {
          v = isArray(v) ? v : v.split(',');
          v = v.slice(0, 3);
          if (v.length === 3) {
            v = `${v[0]}(${v[1]},${v[2]})`;
          } else if (v.length === 2) {
            v = `${v[0]}(${v[1]})`;
          } else {
            v = `${v[0]}`;
          }
        }
        if (realKey === 'YZDZ') {
          v = data.LOCATION || data.YZDZ;
        }
      }
      const replaceStr = keys.includes(realKey) ? `<span style="color: red">${v}</span>` : v;
      str = str.replace(k, replaceStr);
    });
  }
  return str;
}

export const recordColumns: ProColumns[] = [
  {
    title: '发送时间',
    dataIndex: 'send_time',
    align: 'center',
    width: '200px',
    ellipsis: true,
  },
  {
    title: '接收号码',
    dataIndex: 'mobile',
    align: 'center',
    width: '120px',
    ellipsis: true,
  },
  {
    title: '短信内容',
    dataIndex: 'content',
    align: 'center',
    ellipsis: true,
  },
];

export const useSmsExamine = (opt: {
  key: 'wait' | 'approved'; //wait-待审核，approved-已审核
  actionRef: any;
}) => {
  const { key = 'wait', actionRef } = opt || {};

  // 获取短信列表
  const getList = async (values: any) => {
    const { current, pageSize, ...rest } = values;
    // console.log('values', values)
    return getSmsList({
      status: key,
      page_num: current, //当前页码
      page_size: pageSize, //每页数据条数
      ...rest,
    }).then((res) => {
      const { code, data, msg } = res;
      if (code == 0) {
        return {
          data: data.list,
          total: data.total_count,
        };
      } else {
        message.error(msg);
        return {
          data: [],
        };
      }
    });
  };

  // 翻页器
  const pagination: ((false | TablePaginationConfig) & (false | PaginationConfig)) | any =
    usePaginationCache(`list${key}`, {
      pageSize: 10,
      showQuickJumper: false,
      showSizeChanger: false,
    });

  const reload = () => actionRef.current?.reload();

  const handleUpdate = (record: any) => {
    upgradation({ cm_id: record.cm_id }).then((res) => {
      const { code, msg } = res;
      if (code == 0) {
        actionRef.current?.reload();
        message.success('操作成功');
      } else {
        message.error(msg);
      }
    });
  };

  const handleOperate = (status: 'pass' | 'reject' | 'black' | 'shield', record: any) => {
    operate({ id: record.id, status }).then((res) => {
      const { code, msg } = res;
      if (code == 0) {
        actionRef.current?.reload();
        message.success('操作成功');
      } else {
        message.error(msg);
      }
    });
  };

  const handleJump = (record: any) => {
    history.push({
      pathname: '/f/sms/examine/record',
      query: { cm_id: record.cm_id },
    });
  };

  return {
    getList,
    pagination,
    reload,
    handleUpdate,
    handleOperate,
    handleJump,
  };
};
